package com.wk.controller.teacher;

import com.wk.dao.Examdao;
import com.wk.pojo.*;
import com.wk.pojo.Class;
import com.wk.service.Courseservice;
import com.wk.service.Examservice;
import com.wk.service.Itemservice;
import com.wk.utils.responseresult;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: thesis
 * @description: 教师期末考试控制类
 * @author: 王侃
 * @create: 2020-02-25 12:38
 **/
@Controller
@RequestMapping("/tea")
public class Teaexaminationcontroller {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    Examservice examservice;
    @Autowired
    Courseservice courseservice;
    @Autowired
    Itemservice itemservice;

    //跳转到考试信息基本信息界面
    @RequestMapping("/exam/topic/list")
    public String examtopic_list(HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Final_examination> examinations=examservice.selectbymyfinalexam(user.getId());
        model.addAttribute("exams",examinations);
        return "teacher/exam/examtopic_management";
    }

    //跳转到考试添加界面
    @RequestMapping("/exam/topic/add")
    public String examtopic_add()
    {
        return "teacher/exam/examtopic_add";
    }

    //接受Date类型数据
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    //获得所有的班级名称
    @ModelAttribute("Allclass")
    public List<Class> getallclass()
    {
        return examservice.selectbyallclass();
    }

    //获得所有习题类型
    @ModelAttribute("Alltypes")
    public List<Item_type> getalltype()
    {
        return itemservice.selectbyitemtype();
    }

    //添加新的课程基本信息到数据库
    @RequestMapping("/exam/topic/addsubmit")
    public String examtopic_addsubmit(Final_examination examination, HttpSession session)
    {
        //获得用户的session信息
        User user=(User)session.getAttribute("loginuser");
        examination.setStatus(0);
        examination.setUser_id(user.getId());
        examination.setUsername(user.getUsername());
        //存入信息加入数据库
        examservice.insertintoFinal_exam(examination);
        return "common/success";
    }

    //修改基本课程信息的状态
    @RequestMapping("/exam/topic/change_status")
    @ResponseBody
    public responseresult change_status(Integer id, Integer status)
    {
        if (status==0)
        {
            status=1;
        }
        else
        {
            status=0;
        }
        examservice.updatebyexam_status(id,status);
        return responseresult.success();
    }

    //修改基本课程信息前获得全部信息
    @RequestMapping("/exam/topic/getinformation")
    public String topic_getinformation(Integer id,Model model)
    {
        Final_examination exam=examservice.selectbytopicid(id);
        model.addAttribute("exam",exam);
        return "teacher/exam/examtopic_update";
    }

    //修改考试基本信息并且提交
    @RequestMapping("/exam/topic/updatesubmit")
    public String topic_updatesubmit(Final_examination examination)
    {
        examservice.updatebyallexamtopic(examination);
        return "common/success";
    }

    //删除考试基本信息，别忘了把所有的题目也删除
    @RequestMapping("/exam/topic/delete")
    public String topic_delete(Integer id)
    {
        examservice.deletebyexamtopic(id);
        examservice.deletebyallcontent(id);
        return "common/success";
    }

    //跳转到考试详细内容管理界面
    @RequestMapping("/exam/content/list")
    public String content_list(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Final_content> contents=examservice.selectbyallcontent(user.getId());
        model.addAttribute("contents",contents);
        List<Final_examination> examinations=examservice.selectbymyfinalexam(user.getId());
        model.addAttribute("exams",examinations);
        return "teacher/exam/examcontent_management";
    }


    //跳转到考试详细内容的添加
    @RequestMapping("/exam/content/add")
    public String content_add(HttpSession session,Model model)
    {
        //获得该教师发布了多少课程
        User user=(User)session.getAttribute("loginuser");
        List<Final_examination> examinations=examservice.selectbymyfinalexam(user.getId());
        model.addAttribute("exams",examinations);

        return "teacher/exam/examcontent_add";
    }

    //实现考试详细内容添加提交
    @RequestMapping("/exam/content/addsubmit")
    public String content_addsubmit(Final_content content, MultipartFile img,HttpSession session)
    {
        logger.info("============>文件上传");
        //如果是图文题
        if (content.getType_id()==4)
        {
            //上传图片和视频并且获得路径
            String image=courseservice.upload(img);
            content.setImage(image);
        }

        User user=(User)session.getAttribute("loginuser");
        content.setUser_id(user.getId());

        //加入数据库
        examservice.insertintofinal_content(content);

        return "common/success";
    }

    //在修改前获得该content的信息
    @RequestMapping("/exam/content/updateget")
    public String content_updateget(HttpSession session,Integer id,Model model)
    {
        //获得该教师发布了多少课程
        User user=(User)session.getAttribute("loginuser");
        List<Final_examination> examinations=examservice.selectbymyfinalexam(user.getId());
        model.addAttribute("exams",examinations);
        Final_content content=examservice.selectbyidcontent(id);
        model.addAttribute("content",content);
        return "teacher/exam/examcontent_update";
    }
    //修改提交content的信息
    @RequestMapping("/exam/content/updatesubmit")
    public String content_updatesubmit(Final_content content,MultipartFile img)
    {
        //如果是图文题
        if (content.getType_id()==4)
        {
            //上传图片和视频并且获得路径
            String image=courseservice.upload(img);
            content.setImage(image);
        }
        examservice.updateintocontent(content);
        return "common/success";
    }

    //删除考试content信息
    @RequestMapping("/exam/content/delete")
    public String content_delete(Integer id)
    {
        examservice.deletebycontent(id);
        return "common/success";
    }

    //进行预览功能
    @RequestMapping("/exam/content/looking")
    public String content_looking(Integer id,Model model)
    {
        Final_examination examination=examservice.selectbytopicid(id);
        model.addAttribute("exam",examination);

        List<Final_content> contents=examservice.selectbyexam_content(id);
        model.addAttribute("contents",contents);

        return "teacher/exam/exam_example";
    }

    //进入到批改作业的首页
    @RequestMapping("/exam/correct/topic")
    public String correct_topic(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        //获得该教师发布了多少课程
        List<Final_examination> examinations=examservice.selectbymyfinalexam(user.getId());
        model.addAttribute("exams",examinations);
        List<Final_stu_results> results=examservice.selectbytea_id_results(user.getId());
        model.addAttribute("results",results);
        return "teacher/exam/exam_correct_management";
    }

    //进行作业批改界面
    @RequestMapping("/exam/correcting/papers")
    public String correcting_papers(HttpSession session,Integer exam_id,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Final_stu_content> contents=examservice.selectbystu_content_exam_id(exam_id);
        model.addAttribute("contents",contents);

        Final_stu_results result=examservice.selectbyidstu_result(exam_id);
        model.addAttribute("result",result);
        //获得该教师发布了多少课程
        List<Final_examination> examinations=examservice.selectbymyfinalexam(user.getId());
        model.addAttribute("exams",examinations);
        return "teacher/exam/exam_correcting_papers";
    }

    //进行作业批改上传至数据库中
    @RequestMapping("/exam/correcting/submit")
    public String correcting_submit(@RequestParam Map<String,String> map,Model model,HttpSession session)
    {
        User user=(User)session.getAttribute("loginuser");
        Set<String> set=map.keySet();//用来获取题目的id号,所以id号为String
        Iterator<String> iterator=set.iterator();//必需的
        Integer flag=0;//标记
        Integer mysocre=0;//得分
        Integer total=0;//总分
        Final_academic academic=new Final_academic();
        while (iterator.hasNext())
        {
            String key=iterator.next();
            if(flag==0)
            {
                academic.setExam_name(map.get(key));
            }
            else if(flag==1){
                academic.setStu_id(Integer.parseInt(map.get(key)));//传入学生Id
            }
            else if (flag==2)
            {
               academic.setStu_name(map.get(key));//传入学生用户名
            }
            else if(flag==3)
            {
                examservice.updatebyid_stu_result_status(Integer.parseInt(map.get(key)));//修改学生提交的试卷已经批改了，不用再批改了
            }
            else
            {
                mysocre+=Integer.parseInt(map.get(key));//累计分数
            }
            flag++;
        }
        academic.setTea_id(user.getId());
        academic.setScore(mysocre);
        //将批改后的成绩存入数据库中
        examservice.insertintoacademic(academic);
        return "common/success";
    }

    //跳转到教师布置的考试信息
    @RequestMapping("/exam/show/students/score")
    public String teacher_score(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Final_academic> list=examservice.selectbytea_id_academic(user.getId());
        model.addAttribute("list",list);
        return "teacher/exam/exam_stu_scores";
    }
}
